package _15_generics._15_8;

/**
 * 泛型数组的创建
 *
 * 因为拆除的原因，数组的创建只能是new Object[]
 * 将Object[] 转为其他类型 就会在运行时报错
 */
public class _06_GenericArray<T> {
    private T[] array;

    public _06_GenericArray(int sz){
        array = (T[]) new Object[sz];
    }

    public void put(int index,T item){
        array[index] = item;
    }

    public T get(int index){
        return array[index];
    }

    public T[] rep(){
        return array;
    }

    public static void main(String[] args) {
        _06_GenericArray<Integer> ga = new _06_GenericArray<>(3);
        //RuntimeException [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

        //编译器错过这类错误的检查
        Integer[] rep = ga.rep();
        Object[] obj = ga.rep();
    }
}
